package com.vip.educenter.controller;


import com.vip.commonutils.JwtUtils;
import com.vip.commonutils.R;
import com.vip.commonutils.ordervo.UcenterMemberOrder;
import com.vip.educenter.entity.UcenterMember;
import com.vip.educenter.entity.vo.RegisterVo;
import com.vip.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hvip
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    // 登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {

        // 返回token值
        String token =  memberService.login(member);

        return R.ok().data("token", token);
    }

    // 注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {

        memberService.register(registerVo);
        return R.ok();
    }

    // 根据token获取信息
    @GetMapping("getMemberInfo")
    public R geMemberInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = memberService.getById(memberId);
            return R.ok().data("userInfo", member);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    //根据客户id获取用户信息,评论
    @PostMapping("getInfo/{id}")
    public R getInfo(@PathVariable String id) {

        UcenterMember ucenterMember = memberService.getById(id);
       // UcenterMember memeber = new UcenterMember();
        //BeanUtils.copyProperties(ucenterMember,memeber);
        return R.ok().data("info",ucenterMember);
    }

    //根据客户id获取用户信息，订单
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {

        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMemberOrder memeber = new UcenterMemberOrder();

        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

    // 查询某一天的注册人数
    @GetMapping("coutRegister/{day}")
    public R coutRegister(@PathVariable String day) {

        Integer count = memberService.coutRegisterDay(day);
        return R.ok().data("coutRegister", count);
    }

    // 修改用户信息
    @PostMapping("setMemberInfo")
    public R setUserInfoOrder(@RequestBody UcenterMember member ) {
        System.out.println(member +"aaaaaaa");
        boolean save = memberService.updateById(member);
        if (save)
            return R.ok();
        else
            return R.error();
    }

}

